import Cookies from 'js-cookie'

const app = {
  state: {
    menu: {},
    sidebar: {
      opened: Cookies.get('studio-sidebarStatus') === 'true'
    },
    sysInfo: {},
    ws: {
      connection: false,
      statusChange: {},
      notification: {}
    }
  },
  mutations: {
    UPDATE_INFO: (state, sysInfo) => {
      state.sysInfo = sysInfo
    },
    TOGGLE_CONNECT_STATUS: (state, isConnected) => {
      state.ws.connection = isConnected
    },
    UPDATE_DEVICE_CHANGE_STATUS: (state, statusChange) => {
      state.ws.statusChange = statusChange
    },
    UPDATE_NOTIFICATION: (state, newNotification) => {
      state.ws.notification = newNotification
    },
    TOGGLE_SIDEBAR: state => {
      // if (state.sidebar.opened) {
      //   Cookies.set('sidebarStatus', 1)
      // } else {
      //   Cookies.set('sidebarStatus', 0)
      // }
      state.sidebar.opened = !state.sidebar.opened
      Cookies.set('studio-sidebarStatus', state.sidebar.opened)
    },
    UPDATE_CURRENT_MENU: (state, menu) => {
      state.menu = menu
    }
  },
  actions: {
    updateInfo ({
      commit
    }, sysInfo) {
      return new Promise(resolve => {
        commit('UPDATE_INFO', sysInfo)
        resolve()
      })
    },
    updateWsConnection ({
      commit
    }, isConnected) {
      return new Promise(resolve => {
        commit('TOGGLE_CONNECT_STATUS', isConnected)
        resolve()
      })
    },
    updateStatus ({
      commit
    }, statusChange) {
      return new Promise(resolve => {
        commit('UPDATE_DEVICE_CHANGE_STATUS', statusChange)
        resolve()
      })
    },
    updateNotification ({
      commit
    }, newNotification) {
      return new Promise(resolve => {
        commit('UPDATE_NOTIFICATION', newNotification)
        resolve()
      })
    },
    ToggleSideBar: ({
      commit
    }) => {
      commit('TOGGLE_SIDEBAR')
    },
    updateCurrentMenu ({
      commit
    }, menu) {
      return new Promise(resolve => {
        commit('UPDATE_CURRENT_MENU', menu)
        resolve()
      })
    }
  }
}

export default app
